package com.intellekt.ecertws.ws.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import un.unece.uncefact.data.standard.reusableaggregatebusinessinformationentity._10.SPSConsignmentItemType;
import un.unece.uncefact.data.standard.reusableaggregatebusinessinformationentity._10.SPSTradeLineItemType;
import un.unece.uncefact.data.standard.spscertificate._6.SPSCertificateType;

import com.intellekt.ecertws.ws.dao.CertDao;
import com.intellekt.ecertws.ws.endpoint.EcertServiceEndpoint;

@Service
public class CertServiceImpl implements CertService {

	private static final Logger log = Logger.getLogger(CertServiceImpl.class);

	@Autowired
	private CertDao dao;
	

	@Override
	public List<SPSCertificateType> getCertificatesByInfo(String from, String to, String issueCountry, String issueLocation, String name) throws Exception {
		List<SPSCertificateType> certs = dao.getCertificatesByInfo(from, to, issueCountry);

		List<SPSCertificateType> certificadosFiltrados = new ArrayList<SPSCertificateType>();

		for (int index = 0; index < certs.size(); index++) {
			String id = certs.get(index).getSPSExchangedDocument().getID();

			List<String> mercs = dao.getMercanciasPermitidas(name);

			List<SPSTradeLineItemType> items = dao.getCertificateItems(id, mercs);

			if (items != null) {
				SPSCertificateType tmp = certs.get(index);
				
				if (log.isDebugEnabled()) {
					log.debug("items -> " + items.size());
				}

				SPSConsignmentItemType cit = new SPSConsignmentItemType();

				for (SPSTradeLineItemType item : items) {
					cit.getIncludedSPSTradeLineItem().add(item);
				}

				tmp.getSPSConsignment().getIncludedSPSConsignmentItem().add(cit);
				
				certificadosFiltrados.add(tmp);

			}

		}

		return certificadosFiltrados;
	}

	@Override
	public List<SPSCertificateType> getCertificatesById(String idCert, String name) {
		List<SPSCertificateType> certs = dao.getCertificatesById(idCert);

		List<SPSCertificateType> certificadosFiltrados = new ArrayList<SPSCertificateType>();

		for (int index = 0; index < certs.size(); index++) {
			String id = certs.get(index).getSPSExchangedDocument().getID();

			// TODO USUARIO TRANSACCION VIDA SILVESTRE
			List<String> mercs = dao.getMercanciasPermitidas(name);
			if (log.isDebugEnabled()) {
				log.debug("mercs -> " + mercs.size());
			}
			List<SPSTradeLineItemType> items = dao.getCertificateItems(id, mercs);

			if (items != null) {
				SPSCertificateType tmp = certs.get(index);
				
				if (log.isDebugEnabled()) {
					log.debug("items -> " + items.size());
				}

				SPSConsignmentItemType cit = new SPSConsignmentItemType();

				for (SPSTradeLineItemType item : items) {
					cit.getIncludedSPSTradeLineItem().add(item);
				}

				tmp.getSPSConsignment().getIncludedSPSConsignmentItem().add(cit);
				
				certificadosFiltrados.add(tmp);

			}

		}

		return certificadosFiltrados;

	}

}
